home *** CD-ROM | disk | FTP | other *** search
/ Digital Information Mana…ntial Guide to Multimedia / Digital Information Management - An Essential Guide to Multimedia.iso / Audacity / Nyquist / follow.lsp < prev    next >
Lisp/Scheme  |  2006-05-16  |  3KB  |  71 lines

  1. ;(set-control-srate 100)
  2. ;(set-sound-srate 100)
  3.  
  4. ;(setf xx (pwl 0 1  1 0 1.1 1 1.8 0 2 1 3 0 5))
  5. ;(setf xx (pwl 0 1  1 .2 1.1 1 1.8 .2 2 1 3 0 5))
  6.  
  7. ;(setf yy (snd-follow xx 0.1 0.25 1.0 30))
  8.  
  9. ;(setf db-factor (/ 1.0 (log 0.00001)))
  10.  
  11.  
  12. ; COMPRESS-MAP -- constructs a map for the compress function
  13. ;
  14. ; The map consists of two parts: a compression part and an expansion part.
  15. ; The intended use is to compress everything above compress-threshold by
  16. ; compress-ratio, and to downward expand everything below expand-ratio
  17. ; by expand-ratio.  Thresholds are in dB and ratios are dB-per-dB.
  18. ; 0dB corresponds to an amplitude of 1.0
  19. ; If the input goes above 0dB, the output can optionally be limited
  20. ; by seting limit-flag to T. This effectively changes the compression
  21. ; ratio to infinity at 0dB.  If limit-flag is NIL, then the compression-ratio
  22. ; continues to apply above 0dB.
  23. ; It is assumed that expand-threshold <= compress-threshold <= 0
  24. ; The gain is unity at 0dB so if compression-ratio > 1, then gain
  25. ; will be greater than unity below 0dB
  26.  
  27. ;(defun compress-map (compress-ratio compress-threshold expand-ratio 
  28. ;             expand-threshold limit-flag)
  29. ;  (let ()
  30. ;    (
  31. ;; I'm not sure if the rest of this function was lost due to version
  32. ;; problems, or it never existed. Email to rbd@cs.cmu.edu if you would
  33. ;; like some help with dynamics compression.
  34. ;;
  35. ;; Also, I had a really great 2-stage compressor for speech -- it did
  36. ;; something like a noise gate with a short time constant, and an automatic
  37. ;; gain control with a long time constant. Each one varied the gain by
  38. ;; about 12 dB -- any more would cause really ugly noise pumping, but
  39. ;; without the combined actions of both, there was not enough control.
  40. ;; Again, email me if you are interested.  Lately, I've been using
  41. ;; more sophisticated multiple band noise reduction in Cool Edit. They
  42. ;; obviously put a lot of work into that, and I don't plan to redo the
  43. ;; work for Nyquist. -RBD
  44.  
  45.  
  46. (defun compress (input map rise-time fall-time)
  47.   ; take the square of the input to get power
  48.   (let ((in-squared (mult input input)))
  49.     ; compute the time-average (sort of a low-pass) of the square
  50.     (setf avg (snd-avg in-squared 1000 500))
  51.     ; use follower to anticipate rise and trail off smoothly
  52.     (setf env (snd-follow avg 0.001 0.2 1.0 20))
  53.     ; take logarithm to get dB instead of linear
  54.     (setf logenv (snd-log env))
  55.     ; tricky part: map converts dB of input to desired gain in dB
  56.     ; this defines the character of the compressor
  57.     (setf shaped-env (shape logenv map 1.0))
  58.     ; go back to linear
  59.     (setf gain (snd-exp shaped-env))
  60.     ; return the scaled input sound,
  61.     ; another trick: avg signal will be delayed. Also, snd-follow
  62.     ; has a delayed response because it's looking ahead in sound
  63.     ; 20 = the number of samples of lookahead from snd-follow
  64.     ; 88.2 = 44,100 (sample rate) / 500 (the step-size in avg)
  65.     ; in other words, 44100/500 is the sample rate of the control
  66.     ; signal looked at by follow
  67.     ; "44100" should be replace by the signal's sample rate
  68.     ; = (snd-srate input)
  69.     (mult (seq (s-rest (/ 20.0 88.2)) (cue input)) gain)))
  70.  
  71.